﻿using System;

namespace CarBooker.Infrastructure.DomainBase
{
    public abstract class EntityBase
    {
        private object key;
        protected EntityBase() : this(null) 
        { 
        }
        protected EntityBase(object key)
        {
            this.key = key;
        }
        public object Key 
        {
            get 
            {
                return this.key;
            }
        }

        #region Equality
        public override bool Equals(object entity)
        {
            if(entity==null || !(entity is EntityBase))
            {
                return false;
            }
            return (this==(EntityBase)entity);
        }
        public static bool operator ==(EntityBase base1, EntityBase base2)
        {
            if ((object)base1 == null && (object)base2 == null)
            {
                return true;
            }
            if ((object)base1 == null || (object)base2 == null)
            {
                return false;
            }
            if (base1.Key != base2.Key)
            {
                return false;
            }
            return true;
        }

        public static bool operator !=(EntityBase base1, EntityBase base2)
        {
            return (!(base1 == base2));
        }
        public override int GetHashCode()
        {
            return this.key.GetHashCode();
        }
        #endregion Equality
    }
}